package com.setup.trac.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.setup.trac.pojo.Progetti;
import com.setup.trac.pojo.Utenti;
import com.setup.trac.pojo.condition.FinderProgettiByIds;
import com.setup.trac.service.SearchService;
import com.setup.trac.util.Browser;
import com.setup.trac.util.SQLCondition;

public class SearchUtenteController extends SimpleFormController {

	private SearchService searchService;
	private Browser browser;

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mav = new ModelAndView("cercaUtente");
		Utenti[] utentis = null;
		if (request.getParameter("cerca") != null) {
			final String nome = request.getParameter("nome");
			final String cognome = request.getParameter("cognome");
			final String email = request.getParameter("email");
			final String progetto = request.getParameter("progetto");
			SQLCondition utenteByFilter = new SQLCondition() {

				public Criterion create(Object... params) {
					Criterion cFull = null;
					Criterion cProgetto = null;
					Criterion cNome = null;
					Criterion cCognome = null;
					Criterion cEmail = null;
					List<Criterion> criterions = new ArrayList<Criterion>();
					// Criterion cProgetto =null;
					if (nome != null && !nome.trim().equals("")) {
						cNome = Restrictions.ilike("nome", nome);
						criterions.add(cNome);
					}
					if (cognome != null && !cognome.trim().equals("")) {
						cCognome = Restrictions.ilike("cognome", cognome);
						criterions.add(cCognome);
					}
					if (email != null && !email.trim().equals("")) {
						cEmail = Restrictions.ilike("email", email);
						criterions.add(cEmail);
					}

					if (criterions.size() == 0) {
						return null;
					}
					for (Iterator iterator = criterions.iterator(); iterator
							.hasNext();) {
						Criterion criterion = (Criterion) iterator.next();
						if (cFull == null) {
							cFull = criterion;
						} else {
							cFull = Restrictions.and(cFull, criterion);
						}
					}
					return cFull;
				}

			};
			Page thePage = getBrowser().impaginaRicerca(request,
					utenteByFilter, Utenti.class);

			utentis= getSearchService().find(new Utenti[0],
					utenteByFilter, thePage.getFirstRecord(), Utenti.class);
			List<Utenti> result = new ArrayList<Utenti>();

			for (int i = 0; i < utentis.length; i++) {
				boolean add = true;
				Utenti u = utentis[i];
				if (progetto != null && !progetto.equals("-1")) {
					Set<Progetti> progettis = u.getProgettis();
					Iterator<Progetti> itProgettis = progettis.iterator();
					for (Iterator iterator = progettis.iterator(); iterator
							.hasNext();) {
						Progetti p = (Progetti) iterator.next();
						if(!p.getIdProgetto().toPlainString().equals(progetto)){
							add=false;
						}else{
							add=true;
						}
					}
				}
				if(add){
					result.add(u);
				}
			}
		}else{
			Page thePage = getBrowser().impaginaRicerca(request,
					null, Utenti.class);

			utentis = getSearchService().find(new Utenti[0], thePage.getFirstRecord(), Utenti.class);

		}
		request.setAttribute("listaUtenti", utentis);
		return mav;
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors, Map controlModel)
			throws Exception {
//		if(request.getParameter("cerca")!=null){
			return onSubmit(request, response, null, errors);
//		}
//		ModelAndView mav = new ModelAndView("cercaUtente");
//		
//		return mav;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new Utenti();
	}

	public Browser getBrowser() {
		return browser;
	}

	public void setBrowser(Browser browser) {
		this.browser = browser;
	}

}
